package de.tum.in.protpred;


import java.util.Arrays;

import weka.classifiers.functions.LibSVM;
import weka.classifiers.meta.CVParameterSelection;
import weka.core.Instances;
import weka.core.SelectedTag;
import weka.core.Utils;
import weka.core.converters.ConverterUtils.DataSource;

public class firstTest {

	
	//private static String set0 = "C:/Users/steckl/TUM/am_neusten/features_manual_2.arff";
	private static String set0 = "C:/Users/steckl/TUM/am_neusten/features_klein.arff";
	
	private static String optimizerArgument = "C 5 15 5";
	
	public static void main(String[] args) throws Exception{
		
		try
		{
			long startTime = java.lang.System.currentTimeMillis();
			
			System.out.println("Start-Time: "+startTime);
			
			if(args.length > 0)
			{
				set0 = args[0];
				
				if(args.length > 1)
				{
					optimizerArgument=args[1];
				}
			}
			else
			{
				System.out.println("You can specify the path to the used arff-file (default: \""+set0+"\") and the used optimisation parameters (default: \""+optimizerArgument+"\") as commandline arguments. Argument 1 is location of arff-file, argument 2 is the string that is passed to the optimizer.");
			}
			
			//open arff file
			 DataSource source = new DataSource(set0);
			 Instances data = source.getDataSet();
			 
			 // setting class attribute if the data format does not provide this information
			 // For example, the XRFF format saves the class attribute information as well
			 if (data.classIndex() == -1)
			 {
			   data.setClassIndex(data.numAttributes() - 1);
			 }
			 
			 data.deleteAttributeAt(0);
			 
			// setup classifier
			 SelectedTag t = new SelectedTag(LibSVM.SVMTYPE_C_SVC,LibSVM.TAGS_SVMTYPE);
			 
			 LibSVM classifierToOptimise = new LibSVM();
			 classifierToOptimise.setSVMType(t);
			 classifierToOptimise.setCost(1);
			 classifierToOptimise.setGamma(8.000000000000001E-4);
			 //classifierToOptimise.setKernelType(new SelectedTag(LibSVM.KERNELTYPE_RBF, LibSVM.TAGS_KERNELTYPE)); 
			 classifierToOptimise.setShrinking(false); // vll schneller
			 CVParameterSelection ps = new CVParameterSelection();		 
			 
			 ps.setClassifier(classifierToOptimise);
			 ps.setNumFolds(5);  // using 5-fold CV
			 //ps.addCVParameter("G 0.0 0.03 30");
			 ps.addCVParameter("C 3.0 5.0 5.0");
			 ps.addCVParameter("G 0.004 0.015 5.0");
             //ps.addCVParameter(optimizerArgument); 
			 // build and output best options
			 
			 ps.buildClassifier(data);
			 
			 classifierToOptimise.setOptions(ps.getBestClassifierOptions());
			 
			 classifierToOptimise.buildClassifier(data);
			
			 ps.getOptions().toString();
			 
			 
			 
			 
			 System.out.println("Best Parameters (optimised for C): "+Utils.joinOptions(ps.getBestClassifierOptions()));
			 System.out.println("file:"+set0);
			 System.out.println("Call: "+Arrays.toString(ps.getOptions()));
			 
			 long finishTime = java.lang.System.currentTimeMillis();
			 
			 System.out.println("Test ran from "+startTime+" until "+finishTime +" -> duration:"+((finishTime-startTime)/1000f)+"s");
		}
		catch(Exception e)
		{
	           e.printStackTrace();
		}
		
	}
	
}

